/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-05-16
 * Time: 21:47
 */
class Solution {
    public int triangleNumber(int[] nums) {
        //1.排序
        Arrays.sort(nums);

        int count = 0;
        for (int i = (nums.length - 1); i >= 2; i--) {
            //2.固定最大数
            int max = nums[i];
            int left = 0;
            int right = i - 1;
            //3,双指针求个数
            while (left < right) {
                if (nums[left] + nums[right] <= max) {
                    left++;
                }else {
                    count += right - left;
                    right--;
                }
            }
        }

        return count;
    }
}